﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace BSE.Windows.Test
{
    public class DataGridViewComboAssociatedBoxCell : DataGridViewComboBoxCell
    {
        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

            ComboBox comboBox = (ComboBox)base.DataGridView.EditingControl;

            if (comboBox != null)
            {
                comboBox.DropDownStyle = ComboBoxStyle.DropDown;
                //comboBox.AutoCompleteMode = AutoCompleteMode.Suggest;
                //comboBox.Validating -= new CancelEventHandler(comboBox_Validating);
                
                //comboBox.KeyPress += new KeyPressEventHandler(comboBox_KeyPress);
                comboBox.TextChanged -= new EventHandler(comboBox_TextChanged);
                comboBox.TextChanged += new EventHandler(comboBox_TextChanged);
                //comboBox.Validating += new CancelEventHandler(comboBox_Validating);
 
            }
        }

        protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
        {
            if (value != null)
            {
                string n = (string)value; 
            }
            return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
        }

        void comboBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            DataGridViewComboBoxEditingControl cbo = (DataGridViewComboBoxEditingControl)sender;

            DataGridView grid = cbo.EditingControlDataGridView;
            object value = cbo.Text;

            for (int i = 0; i < cbo.Items.Count; i++)
            {
                Type t = cbo.Items[i].GetType();
                string s = t.GetProperty(cbo.ValueMember).GetValue(cbo.Items[i], null).ToString();
                if (s == cbo.Text)
                {
                    cbo.SelectedIndex = i;
                    break;
                }
            }

            grid.CurrentCell.Value = cbo.SelectedValue;
        }

        protected void comboBox_KeyPress(object sender, KeyPressEventArgs args)
        {
            DataGridViewComboBoxEditingControl cbo = (DataGridViewComboBoxEditingControl)sender;
            if (cbo.Text.Trim() == string.Empty) return;

            DataGridView grid = cbo.EditingControlDataGridView;
            string value = cbo.Text;

            for (int i = 0; i < cbo.Items.Count; i++)
            {
                Type t = cbo.Items[i].GetType();
                string s = t.GetProperty(cbo.ValueMember).GetValue(cbo.Items[i], null).ToString();
                if (s == cbo.Text)
                {
                    cbo.SelectedIndex = i;
                    cbo.Text = cbo.SelectedText;
                    grid.CurrentCell.Value = cbo.SelectedValue;
                    break;
                }
            }
        }

        protected void comboBox_TextChanged(object sender, EventArgs args)
        {
            DataGridViewComboBoxEditingControl cbo = (DataGridViewComboBoxEditingControl)sender;
            if (cbo.Text.Trim() == string.Empty) return;

            DataGridView grid = cbo.EditingControlDataGridView;
            string value = cbo.Text;
            for (int i = 0; i < value.Length; i++)
            {
                if ((value[i] < '0' || value[i] > '9') && (value[i] < 'a' || value[i] > 'z'))
                    return;
            }

            for (int i = 0; i < cbo.Items.Count; i++)
            {
                Type t = cbo.Items[i].GetType();
                string s = t.GetProperty(cbo.ValueMember).GetValue(cbo.Items[i], null).ToString();
                /*string y = "";
                if (t.GetProperty("PersonPYM") != null)
                    y = t.GetProperty("PersonPYM").GetValue(cbo.Items[i], null).ToString();
                if (s == cbo.Text || y == cbo.Text)*/
                if (s == cbo.Text)
                {
                    cbo.SelectedIndex = i;
                    cbo.Text = cbo.SelectedText;
                    grid.CurrentCell.Value = cbo.SelectedValue;
                    break;
                }
            }
        }
    }
}
